$import('ArrayUtil');
$import('CString');
function T(domId){
	return TagUtil.get(domId);
}
var TagUtil = {
	currentId:0,
	createId:function(){
		TagUtil.currentId++;
		return "tag"+TagUtil.currentId;
	},
	get:function(domId){
		var dom =document.getElementById(domId);
		if(dom){
			var tag = new Tag(dom.tagName.toLowerCase());
			tag.domId=domId;
			tag.setDrawed(true);
			return tag;
		}else{
			return null;
		}
	},
	tagArray:[
		"dl","dd","dt","textarea","input",
		"i","p","hr","table","tr",
		"td","th","span","div","ul",
		"li","img","a","em","strong"
	],
	appendMap:{
		tr:function(dom){
			var tr = dom.insertRow(-1);
			this.setDomAttr(tr);
		},
		td:function(dom){
			var td = dom.insertCell(-1);
			this.setDomAttr(td);
		}
	},
	domAttrMap:{
		"class":"className"
	},
	clearMap:{
		table:function(dom){
			if(!dom) return;
			while(dom.rows.length>0){
				dom.deleteRow(0);
			}
		}
	},
	funArray:[]
	
}
var Tag = $createClass("Tag",function(tagName){
	this.tagName = tagName;
	//this.buildAddChildFun("li");
	var tagArray = TagUtil.tagArray;
	var len = tagArray.length;
	
	for(var i=0;i<len;i++){
		this.buildAddChildFun(tagArray[i]);
	}
});
Tag.prototype.onclick=function(fun){
	return this.event("onclick",fun);
}

Tag.prototype.addChilds = function(array,fun){
	if(!array || array.length==0 || !fun) return;
	var self = this;
	ArrayUtil.foreach(array,function(n,pojo){
		self.appendChild(fun(pojo));
	})
	return self;
}

Tag.prototype.getDom = function(){
	return document.getElementById(this.domId);
}

Tag.prototype.css = function(name){
	return this.attr("class",name);
}

Tag.prototype.id = function(name){
	return this.attr("id",name);
}

Tag.prototype.event = function(key,fun){
	if(!this.eventMap)
		this.eventMap = {};
	if(fun && key){
		TagUtil.funArray.push(fun);
		this.eventMap[key] = TagUtil.funArray.length-1;
	}
	if(this.drawed){
		this.setEvent2Dom(this.getDom(),key,fun);
	}
	return this;
}

Tag.prototype.attr = function(key,value){
	if(key=="id"){
		this.domId = value;
	}
	if(!this.attrMap){
		this.attrMap = {};
	}
	this.attrMap[key] = value;
	if(this.drawed){
		this.setAttr2Dom(this.getDom(),key,value);
	}
	return this;
}

Tag.prototype.href = function(url){
	if(!url) url='#';
	return this.attr("href",url);
}

Tag.prototype.style = function(key,value){
	if(!value) return;
	if(!this.styleMap){
		this.styleMap = {};
	}
	this.styleMap[key] = value;
	if(this.drawed){
		this.setStyle2Dom(this.getDom(),key,value);
	}
	return this;
}

Tag.prototype.html = function(html){
	this.innerHTML = html;
	if(this.drawed){
		this.setHtml2Dom(this.getDom(),html);
	}
	return this;
}

Tag.prototype.appendChild = function(tag){
	if(!tag) return ;
	if(tag){
		this.getChildren().push(tag);
		tag.parent = this;
	}
	if(this.drawed){
		tag.append2Dom(this.getDom());
		tag.setDrawed(true);
	}
	return tag;
}
Tag.prototype.getChildren = function(){
	if(!this.childArray)
		this.childArray = [];
	return this.childArray;
}
Tag.prototype.setDrawed = function(drawed){
	this.drawed = drawed;
	ArrayUtil.foreach(this.getChildren(),function(n,pojo){
		pojo.setDrawed(drawed);
	});
}

Tag.prototype.toHtml = function(){
	var cstring = new CString();
	cstring.append("<");
	cstring.append(this.tagName);
	if(this.attrMap){
		var attrMap = this.attrMap;
		for(var e in attrMap){
			cstring.append(" ");
			cstring.append(e);
			cstring.append("='");
			cstring.append(attrMap[e]);
			cstring.append("'");
		}
	}
	
	if(this.eventMap){
		var eventMap = this.eventMap;
		for(var e in eventMap){
			cstring.append(" ");
			cstring.append(e)
			cstring.append("='");
			cstring.append('TagUtil.funArray['+ eventMap[e]+"]()");
			cstring.append("'");
		}
	}
	
	if(this.styleMap){
		var styleMap = this.styleMap;
		var str = "";
		for(var e in styleMap){
			str= str+e+":"+styleMap[e]+";";
		}
		cstring.append(" style='"+str+"'")
	}
	cstring.append(">");
	if(this.childArray){
		var childArray = this.childArray;
		var len = childArray.length;
		for(var i=0;i<len;i++){
			cstring.append(childArray[i].toHtml());
		}
	}else{
		if(this.innerHTML)
			cstring.append(this.innerHTML); 
	}
	cstring.append("</");
	cstring.append(this.tagName);
	cstring.append(">");
	return cstring.toString();
}

Tag.prototype.toDom = function(){
	var dom = document.createElement(this.tagName);
	this.setDomAttr(dom);
	return dom;
}

Tag.prototype.setAttr2Dom = function(dom,key,value){
	if(dom){
		
		if(key=="class"){
			dom.className = value;
		}else{
			dom.setAttribute(key,value);
		}
	}
}
Tag.prototype.setStyle2Dom = function(dom,key,value){
	dom.style[key] = value;
}
Tag.prototype.setEvent2Dom = function(dom,key,value){
	dom[key] = value;
}

Tag.prototype.clearHtml = function(){
	this.innerHTML = "";
	this.childArray = "";
	if(this.drawed){
		this.clearDom(this.getDom());
	}
}

Tag.prototype.clearDom = function(dom){
	if(!dom) return;
	var method = TagUtil.clearMap[this.tagName];
	if(!method){
		dom.innerHTML = "";
	}else{
		method.apply(this,[dom]);
	}
}

Tag.prototype.setHtml2Dom = function(dom,html){
	if(dom ){
		dom.innerHTML = html;
	}
	
}
Tag.prototype.setDomAttr = function(dom){
	if(this.attrMap){
		var attrMap = this.attrMap;
		for(var e in attrMap){
			this.setAttr2Dom(dom,e,attrMap[e]);
		}
	}
	if(this.styleMap){
		var styleMap = this.styleMap;
		for(var e in styleMap){
			this.setStyle2Dom(dom,e,styleMap[e]);
		}
		
	}
	
	if(this.eventMap){
		var eventMap = this.eventMap;
		for(var e in eventMap){
			this.setEvent2Dom(dom,e,eventMap[e]);
		}
	}
	
	if(this.childArray){
		var childArray = this.childArray;
		var len = childArray.length;
		for(var i=0;i<len;i++){
			childArray[i].append2Dom(dom);
		}
	}else{
		if(this.innerHTML)
			dom.innerHTML = this.innerHTML
	}
	
}

Tag.prototype.buildAddChildFun = function(tagName){
	this[tagName] = function(html){
		var tag = new Tag(tagName);
		if(html){
			tag.html(html);
		}
		this.appendChild(tag);
		return tag;
	}
	this["_"+tagName] = function(html){
		var tag = new Tag(tagName);
		if(html){
			tag.html(html);
		}
		this.appendChild(tag);
		return this;
	}
}
Tag.prototype.append2Dom = function(dom){
	if(dom){
		if(!this.domId){
			this.attr("id",TagUtil.createId());
		}
		var method = TagUtil.appendMap[this.tagName.toLowerCase()];
		if(!method){
			this.doAppend2Dom(dom);
		}else{
			method.apply(this,[dom]);
		}
	}
}
Tag.prototype.doAppend2Dom = function(dom){
	dom.appendChild(this.toDom());
}